package hkust.ee.fyp.drawer;

import hkust.ee.fyp.R;
import hkust.ee.fyp.drawer.PhotoframeAdapter.PhotoframeChooserCallback;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ChoosePhotoframe extends Activity implements PhotoframeChooserCallback{

	private PhotoframeChooserCallback m_callback = this;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
	    super.onCreate(savedInstanceState); 
	 
	    setContentView(R.layout.choose_photoframe); 
	 
	    GridView g = (GridView) findViewById(R.id.photoframe_grid_view);
	    PhotoframeAdapter adapter = new PhotoframeAdapter(this);
	    adapter.setCallback( m_callback);
	    g.setAdapter(adapter);
	}

	@Override
	public void onSelectPhotoframe(PhotoframeAdapter sender) {

	    Intent result = new Intent();
		Bundle bundle = new Bundle();
		
		bundle.putInt( "photoframe_resource_id", sender.getSelectedItem());
		result.putExtras( bundle);
		
	    setResult( Activity.RESULT_OK, result);
	    finish();
	}
}

class PhotoframeAdapter extends BaseAdapter implements OnClickListener {

    private Context mContext;
    private PhotoframeChooserCallback m_callback = null;
    private int selectedIndex = -1;
    
    private Integer[] mThumbIds = {
            R.drawable.frame1_release_thumb,
            R.drawable.frame2_release_thumb,
            R.drawable.frame3_release_thumb,
            R.drawable.frame4_release_thumb,
            };
    
    private Integer[] mFrameIds = {
            R.drawable.frame1_release,
            R.drawable.frame2_release,
            R.drawable.frame3_release,
            R.drawable.frame4_release,
            };
    
    public PhotoframeAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public int getItemIndex(ImageView imageView)
    {
    	return (Integer) imageView.getTag();
    }
    
    public int getSelectedItem()
    {
    	return mFrameIds[selectedIndex];
    }
    
	public void setCallback(PhotoframeChooserCallback callback) {
		m_callback = callback;
	}

    @Override
    public View getView( int position, View convertView, ViewGroup parent) { 
        final ImageView imageView; 
        if (convertView == null) { 
            imageView = new ImageView(mContext); 
            imageView.setLayoutParams(new GridView.LayoutParams(250, 400));
            imageView.setAdjustViewBounds(false); 
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            
        } else { 
            imageView = (ImageView) convertView; 
        } 
 
        imageView.setImageResource(mThumbIds[position]); 
        imageView.setTag(position);
        imageView.setOnClickListener(this);
        return imageView; 
    }
    
	public interface PhotoframeChooserCallback {
		public void onSelectPhotoframe(PhotoframeAdapter sender);
	}
	
	public void onClick(View v) {
		if(v instanceof ImageView){
			ImageView imageView = (ImageView) v;
			selectedIndex = getItemIndex(imageView);
			m_callback.onSelectPhotoframe(PhotoframeAdapter.this);
		}
	}
}